﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa.GameSound;

namespace Scopa.Menu
{
    public interface IScopaeBriscolaMenu
    {
        event MenuEvent OnScopaClicked;
        event MenuEvent OnBriscolaClicked;
        event MenuEvent OnExitClicked;
    }

    public class ScopaeBriscolaMenu : BaseMenu, IScopaeBriscolaMenu
    {
        private Button scopaBtn, briscolaBtn, exitBtn;
        private SpriteFont largeFont;
        public int colorBtnTimer, direction = 1, counter = 0;
        SpriteBatch spriteBatch;
        private Texture2D TextureLeftScopa, TextureRightBriscola;
        private bool animationFinished = false;
        private float opacity = 1.0f;
        private Color colorScopa, colorBriscola;

        public ScopaeBriscolaMenu(Game game, MenuDepths depth)
            : base(game, depth)
        {
            background = game.Content.Load<Texture2D>("white_pixel");
            bounds = game.GraphicsDevice.Viewport.Bounds;
            game.Services.AddService(typeof(IScopaeBriscolaMenu), this);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IScopaeBriscolaMenu));
            base.Dispose(disposing);
        }


        public override void Initialize()
        {
            spriteBatch = Game.GetScopaContext().SpriteBatch;

            // Edera Texture
            TextureLeftScopa = Game.Content.Load<Texture2D>(@"Images//Menu//LeftScopa");
            TextureRightBriscola = Game.Content.Load<Texture2D>(@"Images//Menu//RightBriscola");

            //Font
            largeFont = Game.Content.Load<SpriteFont>("baveuse1_font");

            // Load all buttons

            // Scopa Button
            scopaBtn = new Button(Game, @"white_pixel", new Rectangle(0, 0, 300, bounds.Height), Color.Gray, "baveuse1_font", "", Color.Black, 0.9f);
            AddButton(scopaBtn, () =>
            {
                var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
                SoundManager.PlaySound(MenuSounds.ClickCard);

                scopaBtn.textColor = Color.Red;
                //colorBtnTimer = 5;
            });

            // Briscola Button
            briscolaBtn = new Button(Game, @"white_pixel", new Rectangle(500, 0, 300, bounds.Height), Color.Gray, "baveuse1_font", "Briscola", Color.Black, 0.9f);
            AddButton(briscolaBtn, () =>
            {
                var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
                SoundManager.PlaySound(MenuSounds.ClickCard);

                briscolaBtn.textColor = Color.Red;
                //colorBtnTimer = 5;
            });

            // Exit Button
            //int strLen = (int)largeFont.MeasureString("Exit").X;
            //exitBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 20, 237, strLen + 40, 70), Color.Transparent, "baveuse1_font", "Exit", Color.Black, 0.9f);
            //AddButton(exitBtn, () =>
            //{
            //    var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
            //    SoundManager.PlaySound(MenuSounds.ClickCard);

            //    exitBtn.textColor = Color.Red;
            //    colorBtnTimer = 5;
            //});


            // Draw variables
            colorScopa = Color.White;
            colorBriscola = Color.White;


            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            if (animationFinished)
            {
                if (scopaBtn.textColor == Color.Red)
                {
                    //colorBtnTimer--;
                    //if (colorBtnTimer < 0)
                    //{
                        if (OnScopaClicked != null)
                            OnScopaClicked();
                    //}
                }

                if (briscolaBtn.textColor == Color.Red)
                {
                    //colorBtnTimer--;
                    //if (colorBtnTimer < 0)
                    //{
                        if (OnBriscolaClicked != null)
                            OnBriscolaClicked();
                    //}
                }

                //if (exitBtn.textColor == Color.Red)
                //{
                //    colorBtnTimer--;
                //    if (colorBtnTimer < 0)
                //    {
                //        if (OnExitClicked != null)
                //            OnExitClicked();
                //    }
                //}
            }
            //else
            //{
                // Animation is not finished
                if (scopaBtn.textColor == Color.Red)
                {
                    colorBriscola.R -= 10;
                    colorBriscola.G -= 10;
                    colorBriscola.B -= 10;
                    if (colorBriscola.R <= 10)
                        animationFinished = true;
                }
                if (briscolaBtn.textColor == Color.Red)
                {
                    colorScopa.R -= 10;
                    colorScopa.G -= 10;
                    colorScopa.B -= 10;
                    if (colorScopa.R <= 10)
                        animationFinished = true;
                }
            //}

            // Input: back button pressed
            if (((Game1)Game).IsBackButtonPressed())
            {
                if (OnExitClicked != null)
                    OnExitClicked();
            }

            base.Update(gameTime);
        }

        private Vector2 input_service_OnTap(Vector2 tapPosition)
        {
            return tapPosition;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            spriteBatch.Begin();

            spriteBatch.Draw(TextureLeftScopa, bounds, colorScopa);
            spriteBatch.Draw(TextureRightBriscola, bounds, colorBriscola);

            spriteBatch.End();
        }

        // Events
        public event MenuEvent OnScopaClicked;
        public event MenuEvent OnBriscolaClicked;
        public event MenuEvent OnExitClicked;
    }
}
